<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>超市管理系统 - 登录</title>
    <!-- jQuery -->
    <script src="./assets/jquery.min.js"></script>
    <!-- Toastr -->
    <link rel="stylesheet" href="assets/toastr.min.css">
    <script src="./assets/toastr.min.js"></script>
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
        }

        body {
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
        }

        .container {
            background: white;
            padding: 40px;
            border-radius: 12px;
            width: 400px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }

        .logo {
            text-align: center;
            margin-bottom: 30px;
        }

        .logo img {
            width: 80px;
            height: 80px;
            margin-bottom: 15px;
        }

        h1 {
            text-align: center;
            color: #2c3e50;
            font-size: 24px;
            margin-bottom: 30px;
            font-weight: 500;
        }

        .form-group {
            margin-bottom: 20px;
            position: relative;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #34495e;
            font-size: 14px;
        }

        .form-group .input-group {
            position: relative;
        }

        .form-group .input-group i {
            position: absolute;
            left: 12px;
            top: 50%;
            transform: translateY(-50%);
            color: #95a5a6;
            font-size: 16px;
        }

        .form-group input {
            width: 100%;
            padding: 10px 12px 10px 35px;
            border: 1px solid #e0e0e0;
            border-radius: 6px;
            font-size: 14px;
            transition: all 0.3s ease;
        }

        .form-group input:focus {
            outline: none;
            border-color: #3498db;
            box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
        }

        .captcha-container {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .captcha-container input {
            flex: 1;
        }

        .captcha {
            height: 38px;
            border-radius: 6px;
            cursor: pointer;
            transition: opacity 0.3s;
        }

        .captcha:hover {
            opacity: 0.8;
        }

        button[type="submit"] {
            width: 100%;
            padding: 12px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            font-size: 15px;
            font-weight: 500;
            transition: background-color 0.3s;
        }

        button[type="submit"]:hover {
            background-color: #2980b9;
        }

        .loading {
            display: none;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(255, 255, 255, 0.8);
            justify-content: center;
            align-items: center;
            border-radius: 12px;
        }

        .loading::after {
            content: '';
            width: 30px;
            height: 30px;
            border: 3px solid #f3f3f3;
            border-top: 3px solid #3498db;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .error-message {
            color: #e74c3c;
            font-size: 12px;
            margin-top: 5px;
            display: none;
        }

        .author-info {
            position: fixed;
            bottom: 40px;
            right: 40px;
            text-align: left;
            color: #566573;
            font-size: 13px;
            line-height: 2;
            background: rgba(255, 255, 255, 0.15);
            padding: 10px 15px;
            border-radius: 8px;
            backdrop-filter: blur(3px);
            -webkit-backdrop-filter: blur(3px);
            border: 1px solid rgba(255, 255, 255, 0.2);
            transition: all 0.3s ease;
            z-index: 1000;
            letter-spacing: 0.5px;
            min-width: 260px;
        }

        .author-info:hover {
            background: rgba(255, 255, 255, 0.25);
            transform: translateY(-2px);
        }

        .author-info p {
            margin: 0;
            opacity: 0.9;
            white-space: nowrap;
        }

        .author-info i {
            margin-right: 8px;
            color: #566573;
            opacity: 0.9;
            width: 16px;
            text-align: center;
        }

        .author-info p:first-child {
            margin-bottom: 2px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="logo">
            <h1>超市管理系统</h1>
        </div>
        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <div class="input-group">
                    <i class="fas fa-user"></i>
                    <input type="text" id="username" placeholder="请输入用户名" required>
                </div>
                <div class="error-message" id="usernameError"></div>
            </div>
            <div class="form-group">
                <label for="password">密码</label>
                <div class="input-group">
                    <i class="fas fa-lock"></i>
                    <input type="password" id="password" placeholder="请输入密码" required>
                </div>
                <div class="error-message" id="passwordError"></div>
            </div>
            <div class="form-group">
                <label for="captcha">验证码</label>
                <div class="captcha-container">
                    <div class="input-group">
                        <i class="fas fa-shield-alt"></i>
                        <input type="text" id="captchaInput" placeholder="请输入验证码" required>
                    </div>
                    <img src="#" class="captcha" id="captcha" alt="验证码">
                </div>
                <div class="error-message" id="captchaError"></div>
            </div>
            <button type="submit">登 录</button>
        </form>
        <div class="loading"></div>
    </div>

    <div class="author-info">
        <p><i class="fas fa-user"></i>作者：李斌，阳鑫，章万磊</p>
        <p><i class="fas fa-chalkboard-teacher"></i>指导老师：张勇</p>
    </div>

    <script>
        $(document).ready(function() {
            // 配置toastr提示框
            toastr.options = {
                "closeButton": true,
                "progressBar": true,
                "positionClass": "toast-top-center",
                "timeOut": "3000"
            };

            let captchaText = [];
            const loginForm = $('#loginForm');
            const loading = $('.loading');

            // 生成验证码
            function generateCaptcha() {
                $.ajax({
                    url: 'http://localhost:9999/user/generateValidateCode',
                    method: 'GET',
                    success: function(response) {
                        if (response.code === 200) {
                            captchaText[0] = response.data.codeKey;
                            captchaText[1] = response.data.codeValue;
                            $('#captcha').attr('src', captchaText[1]);
                        } else {
                            toastr.error('获取验证码失败');
                        }
                    },
                    error: function() {
                        toastr.error('服务器错误，请稍后重试');
                    }
                });
            }

            // 点击验证码图片刷新
            $('#captcha').click(function() {
                generateCaptcha();
            });

            // 页面加载时生成验证码
            generateCaptcha();

            // 表单提交
            loginForm.on('submit', function(e) {
                e.preventDefault();
                
                const loginData = {
                    username: $('#username').val().trim(),
                    password: $('#password').val(),
                    captcha: $('#captchaInput').val().trim(),
                    codeKey: captchaText[0]
                };

                // 显示加载动画
                loading.css('display', 'flex').hide().fadeIn();

                $.ajax({
                    url: 'http://localhost:9999/user/login',
                    method: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(loginData),
                    success: function(response) {
                        if (response.code === 200) {
                            localStorage.setItem('token', response.data.token);
                            window.location.href = 'houtai_gzt.html';
                        } else {
                            toastr.error(response.msg || '登录失败');
                            generateCaptcha();
                            $('#captchaInput').val('');
                        }
                    },
                    error: function() {
                        toastr.error('服务器错误，请稍后重试');
                        generateCaptcha();
                    },
                    complete: function() {
                        loading.fadeOut();
                    }
                });
            });
        });
    </script>
</body>
</html>